<?php
/**
 * Created by PhpStorm.
 * User: liang
 * Email:1050187159@qq.com
 * Date: 2018/8/20
 * Time: 19:59
 */
namespace Home\Controller;

use Think\Controller;

class FileController extends Controller
{
    public function upload()
    {
        //实例化上传类
        $upload = new \Think\Upload();
        //设置附件上传大小
        $upload->maxSize = C('UPLOAD_CONFIG.maxSize');
        //设置附件上传类型
        $upload->exts = C('UPLOAD_CONFIG.exts');
        //设置附件上传根目录
        $upload->rootPath = C('UPLOAD_CONFIG.rootPath');
        //设置附件上传（子）目录
        $upload->savePath = C('UPLOAD_CONFIG.savePath');
        //设置文件保存的文件名
        $upload->saveName = date('YmdHis',time());
        //上传文件
        $info = $upload->uploadOne($_FILES['upload_image']);
        if(!$info) {
            //上传错误提示错误信息
            //$this->ajaxReturn($upload->getError());
            echo json_encode(['code' => 402, 'msg' => $upload->getError()]);
            return false;
        }else{
            //上传成功 获取上传文件信息
            // $this->ajaxReturn(UPLOAD_PATH.$info['savepath'].$info['savename']);
            echo json_encode(['code'=>200, 'src' => UPLOAD_PATH.$info['savepath'].$info['savename']]);
            return false;
        }
    }


}